import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '仪表盘', icon: 'dashboard' }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/carInfo',
    component: Layout,
    redirect: '/carInfo/list',
    name: 'carInfo',
    meta: { title: '车辆管理', icon: 'el-icon-truck' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'carInfoList',
        component: () => import('@/views/CarInfo/list'),
        meta: { title: '车辆列表' }
      },
      {
        path: 'create',
        name: 'carInfoCreate',
        component: () => import('@/views/CarInfo/form'),
        meta: { title: '新增车辆' }
      },
      {
        path: 'edit/:id',
        name: 'carInfoEdit',
        component: () => import('@/views/CarInfo/form'),
        meta: { title: '编辑车辆' },
        hidden: true
      }
    ]
  },
  {
    path: '/emp',
    component: Layout,
    redirect: '/emp/list',
    name: 'emp',
    meta: { title: '员工', icon: 'el-icon-user-solid' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'empList',
        component: () => import('@/views/Emp/list'),
        meta: { title: '员工列表' }
      },
      {
        path: 'create',
        name: 'empCreate',
        component: () => import('@/views/Emp/form'),
        meta: { title: '新增员工' }
      },
      {
        path: 'edit/:id',
        name: 'empEdit',
        component: () => import('@/views/Emp/form'),
        meta: { title: '编辑员工' },
        hidden: true
      }
    ]
  },
  {
    path: '/memberInformation',
    component: Layout,
    redirect: '/memberInformation/list',
    name: 'memberInformation',
    meta: { title: '会员管理', icon: 'el-icon-s-custom' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'memberInformationList',
        component: () => import('@/views/MemberInformation/list'),
        meta: { title: '会员列表' }
      },
      {
        path: 'create',
        name: 'memberInformationCreate',
        component: () => import('@/views/MemberInformation/form'),
        meta: { title: '新增会员' }
      },
      {
        path: 'edit/:id',
        name: 'memberInformationEdit',
        component: () => import('@/views/MemberInformation/form'),
        meta: { title: '编辑会员' },
        hidden: true
      }
    ]
  },
  {
    path: '/balanceRechargeRecord',
    component: Layout,
    redirect: '/balanceRechargeRecord/list',
    name: 'balanceRechargeRecord',
    meta: {
      title: '充值商品管理',
      icon: 'el-icon-shopping-cart-full'
    },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'balanceRechargeRecordList',
        component: () => import('@/views/BalanceRechargeRecord/list'),
        meta: { title: '充值商品列表' }
      },
      {
        path: 'create',
        name: 'balanceRechargeRecordCreate',
        component: () => import('@/views/BalanceRechargeRecord/form'),
        meta: { title: '新增充值商品' }
      },
      {
        path: 'edit/:id',
        name: 'balanceRechargeRecordEdit',
        component: () => import('@/views/BalanceRechargeRecord/form'),
        meta: { title: '编辑充值商品' },
        hidden: true
      }
    ]
  },
  {
    path: '/item',
    component: Layout,
    redirect: '/item/list',
    name: 'item',
    meta: { title: '商品管理', icon: 'el-icon-s-shop' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'itemList',
        component: () => import('@/views/Item/list'),
        meta: { title: '商品列表' }
      },
      {
        path: 'create',
        name: 'itemCreate',
        component: () => import('@/views/Item/form'),
        meta: { title: '新增商品' }
      },
      {
        path: 'edit/:id',
        name: 'itemEdit',
        component: () => import('@/views/Item/form'),
        meta: { title: '编辑商品' },
        hidden: true
      }
    ]
  },
  {
    path: '/itemType',
    component: Layout,
    redirect: '/itemType/list',
    name: 'itemType',
    meta: { title: '商品类别管理', icon: 'el-icon-s-flag' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'itemTypeList',
        component: () => import('@/views/ItemType/list'),
        meta: { title: '商品类别列表' }
      },
      {
        path: 'create',
        name: 'itemTypeCreate',
        component: () => import('@/views/ItemType/form'),
        meta: { title: '新增商品类别' }
      },
      {
        path: 'edit/:id',
        name: 'itemTypeEdit',
        component: () => import('@/views/ItemType/form'),
        meta: { title: '编辑商品类别' },
        hidden: true
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    redirect: '/order/list',
    name: 'order',
    meta: { title: '订单管理', icon: 'el-icon-s-order' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'orderList',
        component: () => import('@/views/Order/list'),
        meta: { title: '订单列表' }
      },
      {
        path: 'create',
        name: 'orderCreate',
        component: () => import('@/views/Order/form'),
        meta: { title: '新增订单' }
      },
      {
        path: 'edit/:id',
        name: 'orderEdit',
        component: () => import('@/views/Order/form'),
        meta: { title: '编辑订单' },
        hidden: true
      }
    ]
  },
  {
    path: '/skill',
    component: Layout,
    redirect: '/skill/list',
    name: 'skill',
    meta: { title: '技能管理', icon: 'el-icon-s-cooperation' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'skillList',
        component: () => import('@/views/Skill/list'),
        meta: { title: '技能列表' }
      },
      {
        path: 'create',
        name: 'skillCreate',
        component: () => import('@/views/Skill/form'),
        meta: { title: '新增技能' }
      },
      {
        path: 'edit/:id',
        name: 'skillEdit',
        component: () => import('@/views/Skill/form'),
        meta: { title: '编辑技能' },
        hidden: true
      }
    ]
  },
  {
    path: '/role',
    component: Layout,
    redirect: '/role/list',
    name: 'role',
    meta: { title: '角色管理', icon: 'el-icon-s-ticket' },
    alwaysShow: true,
    children: [
      {
        path: 'list',
        name: 'roleList',
        component: () => import('@/views/Role/list'),
        meta: { title: '角色列表' }
      },
      {
        path: 'create',
        name: 'roleCreate',
        component: () => import('@/views/Role/form'),
        meta: { title: '新增角色' }
      },
      {
        path: 'edit/:id',
        name: 'roleEdit',
        component: () => import('@/views/Role/form'),
        meta: { title: '编辑角色' },
        hidden: true
      }
    ]
  }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
